﻿using System;
using System.Collections.Generic;
using System.Text;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using Module;
using Cit.CDE.Net.Module.Commons;
using UserControl;

using System.Web.Security;
using System.Web.Configuration;
using System.Xml;
using System.Data;
using Cit.CDE.Net.Service;
using System.Net.Mail;


namespace Global
{
    /// <summary>
    /// Copyright (C)   北京世纪思特技术开发有限公司
    /// FileName:       SendMsgTask.cs
    /// Author :         wangbc
    /// CDT:             2011-04-27           
    /// Version:         1.0.0.0                
    /// Depiction:       发送会员信息，邮件
    /// </summary>
    public class SendMsgTask
    {

        /// <summary>
        /// 培训报告通知
        /// </summary>
        public static void ReportTraingMsg()
        {
            string MSG_CONTENT = "";
            string MSG_TITLE = "";
            string SOURCE_ID = "";
            string MSG_TYPE = "4";

            string sql = @"SELECT * FROM (
                    select 
                    (SELECT COUNT(*) FROM  MESSAGESP WHERE MSG_TYPE=4 AND SOURCE_ID=T.ID) as MSG
                    ,T.*,U.NAME AS TEACHER__NAME,U.EMAIL
                    From TRAINING T,USERS U
                    where T.TRAINING_STATUS in(1,3) and T.REPORT is null AND T.TEACHER=U.ID
                    and  DATEDIFF(DAY,T.END_DATE,GETDATE())>=0
                    ) AS R WHERE R.MSG=0 
                    ";

            DataTable reportDatas = Util.loadSql(sql);
            int rows = reportDatas.Rows.Count;
            if (rows > 0)
            {
                //通知消息对象列表构建
                List<CDEEntity> listEntity_MSG = new List<CDEEntity>();
                CDEEntityManager em_MSG = cde.cache.CDEFactory.getEMByFullName("WEB.MSGP_MANAGE");
                //当前时间
                string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");

                for (int i = 0; i < rows; i++)
                {
                    DataRow row = reportDatas.Rows[i];
                    string RECEIVE_USER = Util.objectToString(row["TEACHER"]);
                    string teacher = Util.objectToString(row["TEACHER__NAME"]);
                    string start_date = Util.objectToString(row["START_DATE"]);
                    DateTime start = DateTime.Parse(start_date);
                    start_date = start.ToShortDateString();
                    string end_date = Util.objectToString(row["END_DATE"]);
                    DateTime end = DateTime.Parse(end_date);
                    end_date = end.ToShortDateString();
                    string msg_content = Util.objectToString(row["MEMO"]);
                    MSG_TITLE = "培训报告填写通知：" + teacher;
                    MSG_CONTENT = start_date + "到" + end_date + "的培训结束，请填写报告！";
                    SOURCE_ID = Util.objectToString(row["PR_ID"]);


                    CDEEntity m_entity_MSG = CDEEMServiceFactory.getEMService(em_MSG).CreateEntity();
                    m_entity_MSG.Operation = CDEDataOperation.INSERT;
                    m_entity_MSG.set("RECEIVE_USER", RECEIVE_USER);

                    //发邮件
                    //-----------------------------------------------------------------------------
                    string EMAIL = Util.objectToString(row["EMAIL"]);
                    try
                    {
                        if (EMAIL != "")
                        {
                            MailMessage mail = new MailMessage();
                            mail.To.Add(EMAIL);
                            mail.Subject = MSG_TITLE;
                            mail.Body = MSG_CONTENT;
                            mail.IsBodyHtml = true;

                            SmtpClient smtp = new SmtpClient();
                            smtp.Send(mail);
                        }
                    }
                    catch (Exception exx)
                    {
                        UserControl.Log.SaveException(exx);
                    }
                    //-----------------------------------------------------------------------------

                    m_entity_MSG.set("INSERT_USER", 2);//系统管理员
                    m_entity_MSG.set("INSERT_DATE", Now);
                    m_entity_MSG.set("MSG_CONTENT", MSG_CONTENT);
                    m_entity_MSG.set("MSG_TITLE", MSG_TITLE);
                    m_entity_MSG.set("SOURCE_ID", SOURCE_ID);
                    m_entity_MSG.set("MSG_TYPE", MSG_TYPE);
                    m_entity_MSG.set("STATE", "0");//1:已读,0:未读

                    listEntity_MSG.Add(m_entity_MSG);
                }

                //更新
                CDEService.UpdateEntities(em_MSG, listEntity_MSG);

                string strErr = string.Empty;
                for (int index = 0; index < listEntity_MSG.Count; index++)
                {
                    if (listEntity_MSG[index].Result == CDEDataOperationResult.ERROR)
                    {
                        strErr = strErr + listEntity_MSG[index].ErrMsg + ",";
                    }
                }
                if (strErr != string.Empty)
                {
                    Log.SaveNote("培训报告提醒发送失败：" + strErr);
                }
            }
        }

        /// <summary>
        /// 市场活动报告通知1:网上申请,2:培训,3:市场活动通知,4:培训报告,5:市场报告
        /// </summary>
        public static void ReportActivityMsg()
        {
            string MSG_CONTENT = "";
            string MSG_TITLE = "";
            string SOURCE_ID = "";
            string MSG_TYPE = "5";//5:市场报告

            string sql = @"SELECT * FROM (
                    select 
                    (SELECT COUNT(*) FROM  MESSAGESP WHERE MSG_TYPE=5 AND SOURCE_ID=T.ID) as MSG
                    ,T.*,U.NAME AS NAME,U.EMAIL
                    From MARKET_ACTIVITY T,USERS U
                    where T.STATUS in(1,3) and T.REPORT is null AND T.MARKETERS=U.ID
                    and  DATEDIFF(DAY,T.END_DATE,GETDATE())>=0
                    ) AS R WHERE R.MSG=0 
                    ";

            DataTable reportDatas = Util.loadSql(sql);
            int rows = reportDatas.Rows.Count;
            if (rows > 0)
            {
                //通知消息对象列表构建
                List<CDEEntity> listEntity_MSG = new List<CDEEntity>();
                CDEEntityManager em_MSG = cde.cache.CDEFactory.getEMByFullName("WEB.MSGP_MANAGE");
                //当前时间
                string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");

                for (int i = 0; i < rows; i++)
                {
                    DataRow row = reportDatas.Rows[i];
                    string RECEIVE_USER = Util.objectToString(row["MARKETERS"]);
                    string teacher = Util.objectToString(row["NAME"]);
                    string start_date = Util.objectToString(row["START_DATE"]);
                    string end_date = Util.objectToString(row["END_DATE"]);
                    string msg_content = Util.objectToString(row["MEMO"]);
                    string titile = Util.objectToString(row["TITILE"]);
                    MSG_TITLE = "市场活动报告填写通知：" + titile;
                    MSG_CONTENT = "市场活动:" + titile + ",活动时间已结束，请填写报告！";
                    SOURCE_ID = Util.objectToString(row["MA_ID"]);


                    CDEEntity m_entity_MSG = CDEEMServiceFactory.getEMService(em_MSG).CreateEntity();
                    m_entity_MSG.Operation = CDEDataOperation.INSERT;
                    m_entity_MSG.set("RECEIVE_USER", RECEIVE_USER);

                    //发邮件
                    //-----------------------------------------------------------------------------
                    string EMAIL = Util.objectToString(row["EMAIL"]);
                    try
                    {
                        if (EMAIL != "")
                        {
                            MailMessage mail = new MailMessage();
                            mail.To.Add(EMAIL);
                            mail.Subject = MSG_TITLE;
                            mail.Body = MSG_CONTENT;
                            mail.IsBodyHtml = true;

                            SmtpClient smtp = new SmtpClient();
                            smtp.Send(mail);
                        }
                    }
                    catch (Exception exx)
                    {
                        UserControl.Log.SaveException(exx);
                    }
                    //-----------------------------------------------------------------------------

                    m_entity_MSG.set("INSERT_USER", 2);//系统管理员
                    m_entity_MSG.set("INSERT_DATE", Now);
                    m_entity_MSG.set("MSG_CONTENT", MSG_CONTENT);
                    m_entity_MSG.set("MSG_TITLE", MSG_TITLE);
                    m_entity_MSG.set("SOURCE_ID", SOURCE_ID);
                    m_entity_MSG.set("MSG_TYPE", MSG_TYPE);
                    m_entity_MSG.set("STATE", "0");//1:已读,0:未读

                    listEntity_MSG.Add(m_entity_MSG);
                }

                //更新
                CDEService.UpdateEntities(em_MSG, listEntity_MSG);

                string strErr = string.Empty;
                for (int index = 0; index < listEntity_MSG.Count; index++)
                {
                    if (listEntity_MSG[index].Result == CDEDataOperationResult.ERROR)
                    {
                        strErr = strErr + listEntity_MSG[index].ErrMsg + ",";
                    }
                }
                if (strErr != string.Empty)
                {
                    Log.SaveNote("市场活动报告提醒发送失败：" + strErr);
                }
            }
        }
          
    }
}